#include <iostream>
#include <fcntl.h>
#include <unistd.h>

using std::cout;
using std::endl;
using std::cerr;

void setIOnoBlock(int fd)
{
    // 获得有关文件描述符的标识位
    int flag = fcntl(fd, F_GETFL);
    fcntl(fd, F_SETFL, flag | O_NONBLOCK);
}

int main()
{
    char buffer[1024];
    setIOnoBlock(0);
    while(true)
    {
        printf("please enter#");
        fflush(stdout);
        ssize_t n = read(0, buffer, sizeof buffer - 2);
        if(n > 0)
        {
            buffer[n] = 0;
            cout << buffer << endl;
        }
        else if(n == 0)
            cout << "read fild close!" << endl;
        else
        {
            cerr << "read fail" << endl;
            sleep(1);
        }
    }
}